package org.ocelot.tunes4j.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JTextField;

public class RoundField extends JTextField {

	private static final long serialVersionUID = -1890212370885110264L;
	
	public RoundField(int cols) {
        super(cols);
        setOpaque(false);
        setForeground(Color.GRAY);
        Font newTextFieldFont=new Font(getFont().getName(),getFont().getStyle(),11);
        setFont(newTextFieldFont);
        setPreferredSize(new Dimension(190,20));
        setBorder(BorderFactory.createEmptyBorder(2, 3, 2, 3));
    }
 
	@Override
	public Insets getInsets() {
		return new Insets(1,8,0,18);
	}
    
    protected void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        renderHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHints(renderHints);
        g2d.setColor(Color.GRAY);
        g2d.fillRoundRect(0, 0, w, h, h, h);
        g2d.setColor(getBackground());
        g2d.fillRoundRect(2, 2, w-2, h-2, h-2, h-2);
                
        URL url = Testing.class.getResource("../icons/close_pressed.png");  
        try {
			final BufferedImage image = ImageIO.read(url);
			int imagey = (getHeight() - image.getHeight())/2+2;
			int imagex = getBorder().getBorderInsets(this).right;
			imagex = getWidth()-image.getWidth()-imagex-2; 
			g.drawImage(image,imagex,imagey,image.getWidth(),image.getHeight(),null);
		} catch (IOException e) {
		} 
        super.paintComponent(g);
    }


       
}

